/*
   TrimWhiteSpace(sString, iSideToTrim = 3)

   This function returns a copy of the string passed to it having all whitespace characters removed from only the extreme left side,
      from only the extreme right side, or from both extremities of the string.
   iSideToTrim = 1, 2, or 3 corresponds to the choice to trim only the left, only the right, or both sides.  "Both" is the default.
   The only whitespace characters recognized by this function are the ASCII characters with codes below 33, DEL (code 127),
      and the ANSI "non-breaking space" character (code 160).
*/
;======================================================================================================================================
TrimWhiteSpace(sString, iSideToTrim = 3)
{
   ;Validation of input arguments
   if (sString = "" or iSideToTrim < 1 or iSideToTrim > 3)
      return sString

   nSize := StrLen(sString)                  ;An updated variable for string size

   bTrimmedLeft := false
   bTrimmedRight := false

   Loop                           ;Left/Right trim loop
   {
      if (!bTrimmedLeft and iSideToTrim != 2)            ;Condition to trim from the left
      {
         iTrimmingFromTheLeft := 1
         nPos := 1
      }
      else if (bTrimmedLeft and !bTrimmedRight and iSideToTrim > 1)   ;Check if we should also trim the right side of sString.
      {
         iTrimmingFromTheLeft := -1
         nPos := nSize
      }
      else
         break

      Loop                        ;Loop through characters being trimmed.
      {
         if (nSize < 1)
         {
            sString := ""                  ;No characters remaining!
            return sString
         }

         sThisChar := SubStr(sString, nPos, 1)
         nThisChar := Asc(sThisChar)
         if (nThisChar < 33 or nThisChar = 127 or nThisChar = 160)   ;Virtually all of the ASCII/ANSI whitespace characters
         {
            nPos += iTrimmingFromTheLeft
            nSize -= 1
         }
         else
         {                        ;Finished trimming for the current trim-direction
            if (iTrimmingFromTheLeft = 1)
               sString := SubStr(sString, nPos, nSize)
            else
               sString := SubStr(sString, 1, nSize)
            break
         }
      }

      if (iTrimmingFromTheLeft = 1)
         bTrimmedLeft := true
      else
         bTrimmedRight := true
   }

   return sString
}
;======================================================================================================================================
;======================================================================================================================================
/*
   CollapseWhiteSpace(sString)

   This function returns a copy of the string passed to it having any consecutive whitespace characters collapsed into a single space
      character (ASCII code 32).
   The only whitespace characters recognized by this function are the ASCII characters with codes below 33, DEL (code 127),
      and the ANSI "non-breaking space" character (code 160).
*/
;======================================================================================================================================
CollapseWhiteSpace(sString)
{               ;This could be written either for speed or memory size, but not both:  I chose speed.
   sString2 := ""

   bWhite := false
   sChar := ""
   Loop, % StrLen(sString)
   {
      sChar := SubStr(sString, A_Index, 1)
      nChar := Asc(sChar)
      if (nChar < 33 or nChar = 127 or nChar = 160)
         bWhite := true
      else
      {
         if (bWhite)      ;Only append when character is NOT white - also append a space if the PRIOR character WAS white.
         {         ;   But then the last character must be handled separately.
            sString2 := sString2 A_Space sChar
            bWhite := false
         }
         else
            sString2 := sString2 sChar
      }
   }

   if (sChar != "" and (nChar < 33 or nChar = 127 or nChar = 160))
      sString2 := sString2 A_Space

   return sString2
}
;======================================================================================================================================
;======================================================================================================================================
/*
   ContainsWhiteSpace(sString)

   This function returns 0 if sString contains no whitespace characters.
   If sString contains any whitespace character(s), then the function returns the position of the first whitespace character starting
      from the left side of the string.  The first character is considered to have a position of 1.
   This behavior is also useful for testing the return value as a boolean value of true or false.
   The only whitespace characters recognized by this function are the ASCII characters with codes below 33, DEL (code 127),
      and the ANSI "non-breaking space" character (code 160).
*/
;======================================================================================================================================
ContainsWhiteSpace(sString)
{
   iAnyWhiteSpace := 0
   Loop, % StrLen(sString)
   {
      sChar := SubStr(sString, A_Index, 1)
      nChar := Asc(sChar)
      if (nChar < 33 or nChar = 127 or nChar = 160)
      {
         iAnyWhiteSpace := A_Index
         break
      }
   }
   return iAnyWhiteSpace
}